<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogVisible"
    :close-on-click-modal="false"
    v-if="dialogVisible"
    width="30%"
    :before-close="handleClose"
  >
    <div>
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="名称" prop="name">
          <el-input v-model="ruleForm.name"   @keyup.enter.native="submitForm('ruleForm')"
            v-focus></el-input>
        </el-form-item>

        <el-form-item label="系统名称" prop="systemId">
          <el-select
            v-model="ruleForm.systemId"
            placeholder="请选择"
            style="width: 100%"
            @change="selectSysChanged"
          >
            <el-option
              v-for="item in systemOption"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
          <!-- <el-input v-model="ruleForm.sysName"></el-input> -->
        </el-form-item>

        <el-form-item label="环境名称" prop="environmentId">
          <el-select
            v-model="ruleForm.environmentId"
            placeholder="请选择"
            style="width: 100%"
          >
            <el-option
              v-for="item in envOption"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
          <!-- <el-input v-model="ruleForm.envName"></el-input> -->
        </el-form-item>

        <el-form-item label="url" prop="url">
          <el-input v-model="ruleForm.url" @keyup.enter.native="submitForm('ruleForm')"></el-input>
        </el-form-item>

        <el-form-item label="用户名" prop="userName">
          <el-input v-model="ruleForm.userName" @keyup.enter.native="submitForm('ruleForm')"></el-input>
        </el-form-item>

        <el-form-item label="密码" prop="pwd">
          <el-input type="password" v-model="ruleForm.password" @keyup.enter.native="submitForm('ruleForm')"></el-input>
        </el-form-item>
      </el-form>
    </div>
    <span class="dialog-footer">
      <el-button @click="resetForm('ruleForm')">取 消</el-button>
      <el-button type="primary" @click="submitForm('ruleForm')"
        >确 定</el-button
      >
    </span>
  </el-dialog>
</template>

<script>
export default {
  props: {
    systemOption: {
      type: Array,
      default: () => [],
    },
    envOption: {
      type: Array,
      default: () => [],
    },
    title: {
      type: String,
      default: "mysql",
    },
  },
  components: {},
  data() {
    return {
      dialogVisible: false,
      ruleForm: {
        id:'',
        name: "",
        systemId: "",
        environmentId: "",
        url: "",
        userName: "",
        password: "",
      },
      rules: {
        systemId: [
          { required: true, message: "请选择系统名称", trigger: "change" },
        ],
        environmentId: [
          { required: true, message: "请选择环境名称", trigger: "change" },
        ],
        name: [{ required: true, message: "请输入系统名称", trigger: "blur" }],
        url: [{ required: true, message: "请输入URL地址", trigger: "blur" }],
        userName: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  methods: {
    clearSelectValue(){
       this.ruleForm.environmentId="";
    },
    selectSysChanged() {
      this.$emit("selectSysChanged", this.ruleForm.selectSys);
       this.ruleForm.environmentId="";
    },
    open(data) {
      if (data) {
        console.log(data);
        this.ruleForm = { ...data };
      }
      this.dialogVisible = true;
    },
    handleClose() {
      this.resetForm("ruleForm");
    },
    close() {
      this.resetForm("ruleForm");
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$emit("save", {...this.ruleForm});
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.ruleForm = {
        name: "",
        systemId: "",
        environmentId: "",
        url: "",
        userName: "",
        password: "",
      };
      this.$refs[formName].resetFields();
      this.dialogVisible = false;
      this.$emit('updateType');
    },
  },
  created() {},
};
</script>
<style lang="scss" scoped>
.dialog-footer {
  display: flex;
  justify-content: center;
}
</style>
